package com.koo.modules.stock.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koo.common.utils.MyStrUtil;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.Query;
import com.koo.common.vo.BaseQueryVo;
import com.koo.modules.stock.dao.StockTakingDao;
import com.koo.modules.stock.entity.StockTakingEntity;
import com.koo.modules.stock.service.StockTakingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class StockTakingServiceImpl extends ServiceImpl<StockTakingDao, StockTakingEntity> implements StockTakingService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<StockTakingEntity> wrapper = new QueryWrapper<>();

        Object dataForm = params.get("dataForm");
        if (dataForm != null) {
            BaseQueryVo queryVo = JSON.parseObject(dataForm + "", BaseQueryVo.class);
            String queryDates = queryVo.getQueryDates();
            Integer auditStatus = queryVo.getAuditStatus();
            String key = queryVo.getKey();
            String queryField = queryVo.getQueryField();
            Integer selectValue = queryVo.getSelectValue();
            String selectOp = null;
            if (Objects.equals(selectValue, 1)){
                selectOp  = ">"; //&gt;
            }else if(Objects.equals(selectValue, 0)){
                selectOp  = "=";
            }else {
                selectOp  = "<"; //&lt;
            }

            if (queryVo != null) {
                if (MyStrUtil.isNotEmpty(queryDates)) {
                    String[] queryDateArr = queryDates.split(",");
                    String startDate = queryDateArr[0];
                    String endDate = queryDateArr[1];
                    wrapper.ge(MyStrUtil.isNotEmpty(startDate), "taking_date", startDate);
                    wrapper.le(MyStrUtil.isNotEmpty(endDate), "taking_date", endDate);
                }
                wrapper.eq(auditStatus != null, "IFNULL(audit_status, 0)", auditStatus);
                wrapper.apply("book_count "  + selectOp + " check_count");
                wrapper.like(StringUtils.isNotBlank(queryField) && StringUtils.isNotBlank(key), queryField, key);
            }
        }

        IPage<StockTakingEntity> page = new Query<StockTakingEntity>().getPage(params);
        page = baseMapper.selectPage(page, wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<StockTakingEntity> listByStoreId(Long storeId) {
        LambdaQueryWrapper<StockTakingEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MyStrUtil.isNotEmpty(storeId), StockTakingEntity::getStoreId, storeId);
        return this.list(wrapper);
    }
}